/// <reference path="../jquery-1.8.2.min.js" />
/// <reference path="../../Content/JQueryQtipCustom/jquery.qtip.min.js" />
/*
*处理页面的相关脚本
*/
//通用form提交事件处理
//btn:点击按钮
//formId:当前FormId
//errMessage:提交出错处理
//topErrDiv：顶部DIVId,可不传
function ajaxSubmitFormHandle(btn, formId, errMessage, jumpUrl, callBack, args, beforecallBack, bfargs, operateDataBack) {   
    if (!errMessage)
        errMessage = '操作失败';
       $(btn).click(function () {
        if (beforecallBack) {
            if (typeof (beforecallBack) == "function")
                if(!beforecallBack.apply(this, bfargs ? bfargs : []))
                	{
                	return;
                	}
            }
        var $ajaxSubmitForm = $(formId);
        if ($ajaxSubmitForm.valid()) {
        	//加载层
        	var index = layer.load(0, { shade: [0.1,'#333']}); //0代表加载的风格，支持0-2
            $(btn).attr({ "disabled": "disabled" });
            $.ajax({
                url: $ajaxSubmitForm.attr("action"),
                data: $ajaxSubmitForm.serialize(),
                type: 'POST',
                success: function (data) {
                	 $(btn).removeAttr("disabled");
                	layer.close(index);
                    //操作成功的提示信息并且跳转页面
                    if (data.succeed) {
                    	//提示层
	                	layer.msg(data.msg,{time:1500});
                        if (jumpUrl) {
                            setTimeout(function () {  window.location.href = data.para; }, 2000);//等3秒  
                        }
                        //返回数据处理
                        if (operateDataBack) {
                            if (typeof (operateDataBack) == "function") {
                                operateDataBack.apply(this, [data.para]);
                            }
                        }
                        setTimeout(function () { layer.closeAll(); }, 1500);
                        }
                    else {
                    	layer.msg(data.msg,{time:1500});
                    }
                },
                error: function (data) {
                	 $(btn).removeAttr("disabled");
                	layer.close(index);
                   layer.alert('出现异常，请联系管理员', {icon: 5});
                }
            });
        }
    });
}



//多整了冗余代码
function confirmSubmitFormHandle(confirmStr, btn, formId, errMessage, topErrDiv, jumpUrl, callBack, args, beforecallBack, bfargs, operateDataBack) {
    if (!topErrDiv)
        topErrDiv = '#divInfo';
    if (!errMessage)
        errMessage = '操作失败';
    //if (!jumpUrl)
    //    jumpUrl = true;
    $(btn).click(function () {
        if (!confirmStr)
            confirmStr = '确认吗';
        if (confirm(confirmStr)) {
            if (beforecallBack) {
                //if (window.console) console.log(callBack + args);
                if (typeof (beforecallBack) == "function")
                    beforecallBack.apply(this, bfargs ? bfargs : []);
                else {
                    for (var i = 0; i < beforecallBack.length; i++) {
                        beforecallBack[i].apply(this, bfargs ? bfargs[i] : []);
                    }
                }
            }

            var $ajaxSubmitForm = $(formId);
            if ($ajaxSubmitForm.valid()) {
                Loading(); $(btn).attr({ "disabled": "disabled" });
                $.ajax({
                    url: $ajaxSubmitForm.attr("action"),
                    data: $ajaxSubmitForm.serialize(),
                    type: 'POST',
                    success: function (data) {
                        EndLoading();
                        //操作成功的提示信息并且跳转页面
                        if (data.ResultType == 0) {
                            $('.qtip').qtip('destroy');
                            $(topErrDiv).qtip({
                                content: {
                                    text: data.Message,
                                    title: {
                                        button: true
                                    }
                                }
                                , position: {
                                    target: [$('body').width() / 2, 20],
                                    my: 'center center',
                                    at: 'center center'
                                }
                                , show: {
                                    ready: true
                                }
                                 , hide: false
                            });
                            if (jumpUrl) {
                                if (data.SSOLink != null) {//子网站登录要跳转的
                                    if (window.console) console.log(data.SSOLink);
                                    var s = data.SSOLink.split(',');
                                    $.each(s, function (i, val) {
                                        if (val != "") {
                                            var so = document.createElement('SCRIPT');
                                            so.src = val;
                                            document.body.appendChild(so);
                                            //这种方式不一定能全部登录子网站，还是从链接那边生成Token登录来的好。
                                        }
                                    });
                                }
                                setTimeout(function () { $(topErrDiv).qtip('destroy'); window.location.href = data.AppendData; }, 2000);
                            }
                            else {
                                setTimeout(function () { $(topErrDiv).qtip('destroy'); }, 2000);
                            }
                            if (callBack) {
                                //if (window.console) console.log(callBack + args);
                                if (typeof (callBack) == "function")
                                    callBack.apply(this, args ? args : []);
                                else {
                                    for (var i = 0; i < callBack.length; i++) {
                                        callBack[i].apply(this, args ? args[i] : []);
                                    }
                                }
                            }
                            //返回数据处理
                            if (operateDataBack) {
                                if (typeof (operateDataBack) == "function") {
                                    operateDataBack.apply(this, [data.OperateData]);
                                }
                            }
                            setTimeout(function () { $(btn).removeAttr("disabled"); }, 4500);
                        }
                        else {
                            //强制刷新验证码
                            $('#Verifycode').focus();
                            $(topErrDiv).qtip({
                                content: {
                                    text: data.Message,
                                    title: {
                                        button: true
                                    }
                                }
                                , position: {
                                    target: [$('body').width() / 2, 20],
                                    my: 'center center',
                                    at: 'center center'
                                }
                                , style: {
                                    classes: 'qtip-red'
                                }
                                , show: {
                                    ready: true
                                }
                                 , hide: false
                            });
                            //if (window.console) console.log("data" + data);
                            //if (window.console) console.log("data.AppendData" + data.AppendData);
                            $.each(data.AppendData, function (index, valOfElement) {
                                var inputElem = "#" + valOfElement.Key;
                                var errorText = valOfElement.ErrorMessages.join(',');
                                //if (window.console) console.log("inputElem" + inputElem + ',' + errorText);
                                //if (window.console) console.log("qtip" + inputElem);
                                $(inputElem).qtip({
                                    content: { text: errorText },
                                    position: {
                                        my: 'left center',
                                        at: 'right center',
                                        viewport: $(window)
                                    },
                                    show: { ready: true },
                                    hide: false,
                                    style: {
                                        classes: 'qtip-red'
                                    }
                                });
                            });
                            EndLoading(); $(btn).removeAttr("disabled");
                        }
                    },
                    error: function (data) {
                        EndLoading(); $(btn).removeAttr("disabled");
                        alert(errMessage);
                    }
                });
            }
        }
        else {
            return false;
        }
    });
}

//通用ajax处理
function ajaxHandle(u, d, errMessage, topErrDiv, jumpUrl, callBack, args, beforecallBack, bfargs, operateDataBack) {
    if (!topErrDiv)
        topErrDiv = '#divInfo';
    if (!errMessage)
        errMessage = '操作失败';
    Loading();
    if (beforecallBack) {
        //if (window.console) console.log(callBack + args);
        if (typeof (beforecallBack) == "function")
            beforecallBack.apply(this, bfargs ? bfargs : []);
        else {
            for (var i = 0; i < beforecallBack.length; i++) {
                beforecallBack[i].apply(this, bfargs ? bfargs[i] : []);
            }
        }
    }
    $.ajax({
        url: u,
        data: d,
        type: 'POST',
        success: function (data) {
            EndLoading();
            $('.qtip').qtip('destroy');
            //操作成功的提示信息并且跳转页面
            if (data.ResultType == 0) {
                $(topErrDiv).qtip({
                    content: {
                        text: data.Message,
                        title: {
                            button: true
                        }
                    }
                    , position: {
                        target: [$('body').width() / 2, 20],
                        my: 'center center',
                        at: 'center center'
                    }
                    , show: {
                        ready: true
                    }
                     , hide: false
                });
                if (jumpUrl) {
                    setTimeout(function () { $(topErrDiv).qtip('destroy'); window.location.href = data.AppendData; }, 2000);
                }
                else {
                    setTimeout(function () { $(topErrDiv).qtip('destroy'); }, 2000);
                }
                if (callBack) {
                    if (window.console) console.log(args);
                    if (typeof (callBack) == "function")
                        callBack.apply(this, args ? args : []);
                    else {
                        for (var i = 0; i < callBack.length; i++) {
                            callBack[i].apply(this, args ? args[i] : []);
                        }
                    }
                }
                //返回数据处理
                if (operateDataBack) {
                    if (typeof (operateDataBack) == "function") {                      
                        operateDataBack.apply(this, [data.OperateData]);
                    }
                }                
            }
            else {
                $(topErrDiv).qtip({
                    content: {
                        text: data.Message,
                        title: {
                            button: true
                        }
                    }
                    , position: {
                        target: [$('body').width() / 2, 20],
                        my: 'center center',
                        at: 'center center'
                    }
                    , style: {
                        classes: 'qtip-red'
                    }
                    , show: {
                        ready: true
                    }
                     , hide: false
                });
                EndLoading();
            }
        },
        error: function (data) {
            EndLoading();
            alert(errMessage);
        }
    });
}

//通用ajax处理
function confirmHandle(confirmStr, u, d, errMessage, topErrDiv, jumpUrl, callBack, args) {
    if (!confirmStr)
        confirmStr = '确认吗';
    if (confirm(confirmStr)) {
        ajaxHandle(u, d, errMessage, topErrDiv, jumpUrl, callBack, args);
    }
}

//ajax访问路径并设置值
function ajaxGetData(url, data, callBack) {
    $.ajax({
        "url": url,
        "type": "get",
        "data": data,
        "success": function (data) {
            if (callBack) {
                if (typeof (callBack) == "function")
                    callBack.apply(this, [data] ? [data] : []);
                else {
                    for (var i = 0; i < callBack.length; i++) {
                        callBack[i].apply(this, [data] ? [data] : []);
                    }
                }
            }
        },
        "fail": function (data) {
        }//function () { alert("操作失败！"); }
    })
}

//任意动态路径AJAX访问
function ajaxLink(url, data, callBack) {
	var index = layer.load(0, { shade: [0.1,'#333']}); //0代表加载的风格，支持0-2
    $.ajax({
        url: url,
        type: "post",
        dataType: "json",
        traditional: true,//传送数组类型的数据的时候，必须使用dataType="josn"且traditional=true
        data: data,
        success: function (data) {
        	layer.close(index);
           //操作成功的提示信息并且跳转页面
           if (data.succeed) {
           	//提示层
			layer.msg(data.msg, {time : 1500});
				// 返回数据处理
				if (typeof (callBack) == "function") {
					callBack(data.para);
				}
			} else {
				layer.msg(data.msg,{time:1500});
           }
       },
       error: function (data) {
       	layer.close(index);
          layer.alert('出现异常，请联系管理员', {icon: 5});
       }
    })
}




